package CustomUI;

import java.util.ArrayList;
import java.util.Collections;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RelativeLayout;

public class DrawingSurface extends RelativeLayout {

	ArrayList<LayerView> layerViews = new ArrayList<LayerView>();

	public DrawingSurface(Context context, AttributeSet attrs) {
		super(context, attrs);
		//setBackgroundColor(Color.CYAN);
	}

	public void addLayerView(LayerView lV) {
		layerViews.add(lV);

		addView(lV);

	}

	public void removeLayerView(LayerView lV) {
		layerViews.remove(lV);
		removeView(lV);
		return;
	}

	public boolean moveUp(int id) {
		LayerView l;
		Log.d("DrawingSurface:MoveUp", "layerViews.size()=" + layerViews.size()+ "");
		for (int i = 0; i < layerViews.size(); i++) {
			l = layerViews.get(i);
			if (l.getId() == id) {
				Log.d("DrawingSurface:MoveUp", "i=" + i+ "");
				if (i != 0){
					//Collections.swap(layerCells, i, i - 1);
					layerViews.remove(i);
					layerViews.add(i-1, l);
					break;
				}
				else
					return false;
			}
		}
		this.removeAllViews();

		for (int i = 0; i < layerViews.size(); i++) {
			l = layerViews.get(i);
			addView(l);
		}
		
		invalidate();
		return true;
	}

	public boolean moveDown(int id) {
		LayerView l;
		Log.d("DrawingSurface:MoveDown", "layerViews.size()=" + layerViews.size()+ "");
		for (int i = 0; i < layerViews.size(); i++) {
			l = layerViews.get(i);
			if (l.getId() == id) {
				Log.d("DrawingSurface:MoveDown", "i=" + i+ "");
				if (i != layerViews.size() - 1){
				//	Collections.swap(layerViews, i, i + 1);
					layerViews.remove(i);
					layerViews.add(i+1, l);
					break;
				}
				else
					return false;
			}
		}
		this.removeAllViews();

		for (int i = 0; i < layerViews.size(); i++) {
			l = layerViews.get(i);
			addView(l);
		}

		invalidate();
		return true;
		
	}

}
